package room

import (
	"errors"
	"server/common"

	"github.com/mitchellh/mapstructure"
	"github.com/sirupsen/logrus"
)

// -------------------------------------------------------------------------------------------------
//
// -------------------------------------------------------------------------------------------------
func (s *RoomService) checkStopPublishReqData(data common.SFUStopPublishReqData) error {
	// Check if room ID provided
	if data.RoomId == "" {
		return errors.New("room ID is required in stop publish request")
	}

	// Check if user ID provided
	if data.UserId == "" {
		return errors.New("user ID is required in stop publish request")
	}

	// Check media information
	if err := ValidateMediaInfo(data.Media); err != nil {
		return errors.New("invalid media information in stop publish request")
	}

	// Check if room exists
	if !s.roomMgr.RoomExists(data.RoomId) {
		return errors.New("room does not exist")
	}

	// Check if user is in the room
	if !s.roomMgr.UserExists(data.RoomId, data.UserId) {
		return errors.New("user is not in the room")
	}

	return nil
}

// -------------------------------------------------------------------------------------------------
//
// -------------------------------------------------------------------------------------------------
func (s *RoomService) sendStopPublishResponse(rid string, code int, msg string, connId string,
	data common.SFUStopPublishReqData) {
	resMsg := common.Message{
		Cmd: common.SFU_STOP_PUBLISH_RES,
		Rid: rid,
		Data: common.ResMsgData{
			Code: code,
			Msg:  msg,
			Data: common.SFUStopPublishResData{
				RoomId: data.RoomId,
				UserId: data.UserId,
				Media:  data.Media,
			},
		},
	}
	s.dispatcher.Dispatch(resMsg, s.serviceInfo, common.AccessServiceInfo, connId)
}

// -------------------------------------------------------------------------------------------------
//
// -------------------------------------------------------------------------------------------------
func (s *RoomService) sendStopPublishNotify(rid string, roomId string, userId string,
	media common.MediaInfo, connId string) {
	notifyMsg := common.Message{
		Cmd: common.SFU_STOP_PUBLISH_NOTIFY,
		Rid: rid,
		Data: common.SFUStopPublishNotifyData{
			RoomId: roomId,
			UserId: userId,
			Media:  media,
		},
	}
	s.dispatcher.Dispatch(notifyMsg, s.serviceInfo, common.AccessServiceInfo, connId)
}

// -------------------------------------------------------------------------------------------------
//
// -------------------------------------------------------------------------------------------------
func (s *RoomService) handleStopPublishReq(msg common.Message, from common.ServiceInfo,
	connId string) error {
	LOG().WithFields(logrus.Fields{
		"msg":    common.ToJson(msg),
		"from":   from,
		"connId": connId,
	}).Info("Handling SFU Stop Publish Request")

	var data common.SFUStopPublishReqData
	if err := mapstructure.Decode(msg.Data, &data); err != nil {
		LOG().WithError(err).Error("Failed to decode stop publish request data")
		s.sendStopPublishResponse(msg.Rid, 1, "failed to decode request data", connId, data)
		return nil
	}

	if err := s.checkStopPublishReqData(data); err != nil {
		LOG(msg.Rid).WithError(err).Error("Stop publish request data validation failed")
		s.sendStopPublishResponse(msg.Rid, 1, err.Error(), connId, data)
		return nil
	}

	s.sendStopPublishResponse(msg.Rid, 0, "success", connId, data)

	for _, user := range s.roomMgr.GetUserList(data.RoomId) {
		if user.UserId != data.UserId {
			s.sendStopPublishNotify(msg.Rid, data.RoomId, data.UserId, data.Media, user.ConnId)
		}
	}

	return nil
}
